package b6.vacancyshare.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DBLowonganFavorit
{
    public static final String KEY_IDFAVORIT = "idfavorit";
    public static final String KEY_USERNAMEPENANDA = "usernamepenanda";
    
    private static final String TAG = "DBLowonganFavorit";
    
    private static final String DATABASE_NAME = "vacancy-share";
    private static final String DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT = "lowonganpekerjaanfavorit";
    private static final int DATABASE_VERSION = 1;
    
    private static final String DATABASE_CREATE_LOWONGANPEKERJAANFAVORIT=
        "create table LOWONGANPEKERJAANFAVORIT(idfavorit integer primary key not null, "
        + "usernamepenanda text not null);";
    
private final Context context; 
    
    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public DBLowonganFavorit(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }
        
    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE_LOWONGANPEKERJAANFAVORIT);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS pengguna");
            onCreate(db);
        }
    }    
    
    //---opens the database---
    public DBLowonganFavorit open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }
    

    //---insert a Lowongan Favorit into the database---
    public long insertLowonganPekerjaanFavorit(String usernamepenanda) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USERNAMEPENANDA, usernamepenanda);
        return db.insert(DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT, null, initialValues);
    }    

    public boolean deleteLowonganPekerjaanFavorit(long idfavorit) 
    {
        return db.delete(DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT, KEY_IDFAVORIT + 
        		"=" + idfavorit, null) > 0;
    }

    //---retrieves all the lowonganpekerjaanfavorit---
    public Cursor getAllLowonganPekerjaanFavorit() 
    {
        return db.query(DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT, new String[] {
        		KEY_IDFAVORIT, 
        		KEY_USERNAMEPENANDA}, 
                null,
                null,
                null,
                null,
                null);
    }

    //---retrieves a particular lowonganpekerjaanfavorit---
    public Cursor getLowonganPekerjaanFavorit(long idfavorit) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT, new String[] {
                		KEY_IDFAVORIT,
                		KEY_USERNAMEPENANDA
                		}, 
                		KEY_IDFAVORIT + "=" + idfavorit, 
                		null,
                		null,
                		null,
                		null,
                		null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }
    
    //---updates a LowonganPekerjaanFavorit---
    public boolean updateLowonganPekerjaanFavorit(long idfavorit, String usernamepenanda) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_USERNAMEPENANDA, usernamepenanda);
        return db.update(DATABASE_TABLE_LOWONGANPEKERJAANFAVORIT, args, 
                         KEY_IDFAVORIT+ "=" + idfavorit, null) > 0;
    }

}
